﻿using System;
using System.ComponentModel;
using System.Reflection;
using System.Web.UI;

namespace Batik.Web.UI.Controls
{
    public class TextBox<T> : System.Web.UI.WebControls.WebControl, ITextControl //where T : new ()
    {
        [Bindable(false), Description("Set this field if value is mandatory"), Category("Validation")]
        public bool Required { get; set; }

        public T Value { get; set; }

        /// <summary>
        /// Specify render mode
        /// <list type="Edit">Edit mode will render as editable control such as input box</list>
        /// <list type="ReadOnly">ReadOnly mode will render as noneditable control such as label</list>
        /// <list type="Disabled">Disabled mode will render same as edit mode but disabled</list>
        /// </summary>
        [Bindable(false), DefaultValue("Edit")]
        public RenderMode RenderMode { get; set; }
        /// <summary>
        /// Specify data field name for client binding usage
        /// </summary>
        [Bindable(false)]
        public string DataFieldName { get; set; }

        // Render the control. First we raise the event to allow the user to
        // populate the data item. Then we print out the value of the data
        // item by calling ToString() on it.
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            //GenericEventArgs<TDataItem> args = new GenericEventArgs<TDataItem>(new TDataItem());
            //OnStuffing(args);

            this.AddAttributesToRender(writer);

            if (RenderMode == RenderMode.ReadOnly || RenderMode == RenderMode.Hidden)
            {
                using (System.Web.UI.WebControls.Label label = new System.Web.UI.WebControls.Label())
                {
                    label.ID = ID;
                    label.Attributes.Add("b:data-type", typeof(T).FullName);
                    if (!string.IsNullOrEmpty(DataFieldName))
                    {
                        if (typeof(T) == typeof(DateTime))
                        {
                            label.Attributes.Add("sys:innertext", string.Format(System.Globalization.CultureInfo.InvariantCulture, "{{binding {0}, convert=dateToString, dateFormat={1}}}", DataFieldName, "dd MMM yyyy"));
                        }
                        else
                        {
                            label.Attributes.Add("sys:innertext", string.Format(System.Globalization.CultureInfo.InvariantCulture, "{{binding {0}}}", DataFieldName));
                        }
                    }
                    //label.Text = Value.ToString();
                    if (RenderMode == RenderMode.Hidden)
                    {
                        label.Style.Add(HtmlTextWriterStyle.Display, "none");
                        label.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                    }
                    label.RenderControl(writer);
                }
            }
            else
            {
                using (System.Web.UI.WebControls.TextBox textbox = new System.Web.UI.WebControls.TextBox())
                {
                    textbox.ID = ID;
                    textbox.Attributes.Add("b:data-type", typeof(T).FullName);
                    if (!string.IsNullOrEmpty(DataFieldName))
                    {
                        textbox.Attributes.Add("sys:value", string.Format(System.Globalization.CultureInfo.InvariantCulture, "{{binding {0}}}", DataFieldName));
                    }
                    if (RenderMode == RenderMode.Disabled)
                    {
                        textbox.Attributes.Add("disabled", "true");
                    }
                    textbox.RenderControl(writer);
                }
            }
        }

        #region ITextControl Members

        public string Text
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
